<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">任务名</label>
        <el-input v-model="query.taskName" clearable placeholder="任务名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">任务状态</label>
        <el-select v-model="query.status" clearable size="small" placeholder="任务状态" class="filter-item" style="width: 120px">
          <el-option v-for="item in dict.task_status" :key="item.id" :label="item.label" :value="item.value" />
        </el-select>
        <date-range-picker
          v-model="query.createTime"
          start-placeholder="任务创建时间"
          end-placeholder="任务创建时间"
          class="date-item"
        />
        <date-range-picker
          v-model="query.finishTime"
          start-placeholder="任务完成时间"
          end-placeholder="任务完成时间"
          class="date-item"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="任务名">
            <el-input v-model="form.taskName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="分配到的文件路径">
            <el-input v-model="form.fileList" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="file_list中所含有的图片数量">
            <el-input v-model="form.numImage" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="已完成的图片数量">
            <el-input v-model="form.numFinish" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="创建任务的时间">
            <el-input v-model="form.createTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="完成时间">
            <el-input v-model="form.finishTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="已审核通过的图片数量">
            <el-input v-model="form.numChecked" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="0未完成/1完成部分/2等待审核/3审核通过/4审核不通过">
            未设置字典，请手动设置 Radio
          </el-form-item>
          <el-form-item label="markTaskId">
            <el-input v-model="form.markTaskId" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="taskDetail" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column prop="taskName" label="任务名" align="center"/>
        <el-table-column prop="numImage" label="任务中所含有的图片数量" align="center"/>
        <el-table-column prop="do" label="标注完成数" align="center"/>
        <el-table-column prop="auditNum" label="已被审核数" align="center"/>
        <el-table-column prop="in_complete" label="审核不通过数" align="center"/>
        <el-table-column prop="numBoxtotal" label="已标注框数" align="center"/>
        <el-table-column prop="createTime" label="任务起始时间" align="center"/>
        <el-table-column prop="finishTime" label="标注完成时间" align="center"/>
        <el-table-column prop="status" label="任务状态" align="center">
          <template slot-scope="scope">
            {{ dict.label.task_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column label="开始任务" width="150px" align="center">
          <template slot-scope="scope">
            <el-button @click="mark(scope.row)" type="success" :disabled="scope.row.status==5">开始标注</el-button>
          </template>
        </el-table-column>
        <el-table-column label="绩效详情" width="150px" align="center">
          <template slot-scope="scope">
            <el-button @click="merits(scope.row)">点击查询</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog
      title="绩效查询"
      :visible.sync="dialogVisible"
      width="70%"
      @close="cancel"
    >
      <!-- <date-range-picker
        v-model="uploadJsonTime"
        start-placeholder="标注上传时间"
        end-placeholder="标注上传时间"
        class="date-item"
      /> -->
      <el-date-picker
        v-model="uploadJsonTime"
        type="daterange"
        style=" width: 500px"
        range-separator="至"
        value-format="yyyy-MM-dd HH:mm:ss"
        format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始日期"
        end-placeholder="结束日期">
      </el-date-picker>
      <el-radio-group v-model="isAllMerits" style="margin-left: 30%">
        <el-radio :label="false">查询当前任务绩效</el-radio>
        <el-radio :label="true">查询所有任务绩效</el-radio>
      </el-radio-group>
      <el-table
        v-loading="loading"
        ref="meritsTable"
        :data="meritsDetail"
        border
        height="330"
        show-summary
        style="width: 100%;margin-top: 1%">
        <el-table-column
          prop="date"
          label="日期"
          width="100">
        </el-table-column>
        <el-table-column
          prop="taskName"
          label="涉及任务"
          width="600">
        </el-table-column>
        <el-table-column
          prop="imageBox"
          label="标注总框数">
        </el-table-column>
        <el-table-column
          prop="imageNum"
          sortable
          label="标注总版数">
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="queryMerits()">点击查询绩效明细</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getImagesByTaskMarkId,getMerits} from '@/api/task/taskassign'
import crudTaskMark from '@/api/system/taskMark'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import DateRangePicker from '@/components/DateRangePicker'
import pagination from '@crud/Pagination'
import store from  '../../store'

const defaultForm = { markTaskId: null, taskName: null, fileList: null, numImage: null, numFinish: null, createTime: null, finishTime: null, numChecked: null, status: null }
export default {
  name: 'TaskMark',
  store,
  components: { pagination, crudOperation, rrOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['task_status','audit_status'],
  cruds() {
    return CRUD({ title: 'api/taskMark', url: 'api/taskMark', idField: 'markTaskId', sort: ['status,asc', 'markTaskId,desc'], crudMethod: { ...crudTaskMark }, optShow: { add: false, reset: true, edit: false, del: false }})
  },
  inject:['reload'],
  data() {
    return {
      hj:{
        name:'111'
      },
      loading: false,
      meritsDetail: null,     //绩效详情
      queryTaskId: null,    //当前选中的任务ID
      isAllMerits: false, //是否查该任务所属人员的所有绩效
      queryUserId: null,
      uploadJsonTime: null,
      dialogVisible: false,
      userId: store.state.user.user.id,
      taskDetail: [],
      permission: {
        add: ['admin', 'taskMark:add'],
        edit: ['admin', 'taskMark:edit'],
        del: ['admin', 'taskMark:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'taskName', display_name: '任务名' },
        { key: 'numImage', display_name: 'file_list中所含有的图片数量' }
      ]
    }
  },
  created() {
    console.log("哈哈哈哈哈")
    console.log(this.hj.name)
    this.hj.name = process.env.VUE_APP_FLASK_API + "/upload/chunk"
    console.log(this.hj.name)
  },
  updated() {
    if(this.dialogVisible){
      this.$nextTick(() => {
        this.$refs['meritsTable'].doLayout();
      })
    }
  },
  methods: {
    //点击”点击查询绩效明细“
    queryMerits(row){
      this.loading = true
      let data = {}
      data.uploadJsonTime = this.uploadJsonTime
      //查当前任务绩效  即传任务id，用户id,isAllMerits和时间
      if (this.isAllMerits==false){
        data.userId = this.queryUserId
        data.markTaskId = this.queryTaskId
        data.isAllMerits = 0
        getMerits(data).then(res => {   //带时间范围和任务id去查
          this.meritsDetail = res
          if (res.length==0) throw err;
          this.loading = false
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '暂无数据'
          })
          this.loading = false
        })
      }
      //查询当前用户所有任务绩效，即只传用户id，isAllMerits和时间
      else{
        data.userId = this.queryUserId
        data.isAllMerits = 1
        getMerits(data).then(res => {
          this.meritsDetail = res
          if (res.length==0) throw err;
          this.loading = false
        }).catch(() => {
          this.loading = false
          this.$message({
            type: 'error',
            message: '暂无数据'
          })
        })

      }
    },
    cancel() {
      this.dialogVisible = false
      this.meritsDetail = null
    },
    merits(row){
      this.queryUserId = row.users[0].id
      this.queryTaskId = row.markTaskId
      this.dialogVisible = true
      let date = new Date();
      let defaultYear = date.getFullYear()
      let defaultMonth = date.getMonth()
      let defaultEndDay = date.getDate() + 1
      /*默认为本月一号到当天*/
      this.uploadJsonTime = [new Date(defaultYear, defaultMonth-1, 1, 0, 0),
                              new Date(defaultYear, defaultMonth, defaultEndDay, 0, 0)]
    },
    mark(row) {
      if(this.$route.path === '/mark/marking' )
      {
        console.log('reload')
        // this.$parent.reload()
        this.reload()
      }
      // this.$router.push(row)
      // this.$router.push({name: 'taskcheck', query: { task: row }})
      console.log(row.status)
      this.$store.commit('SET_MARK_FLAG_TRUE')
      getImagesByTaskMarkId (row.markTaskId).then( res => {
        this.$router.push({
          path: 'marking',
          // params: { data: '222' },
          query: { row: res, taskMarkId: row.markTaskId }
        })
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterToCU]() {

    },
    [CRUD.HOOK.afterRefresh]() {
      //  数据处理
      this.taskDetail = []
      for (let i = 0; i < this.crud.data.length; i++) {
        const taskDetail = {}
        // 任务名
        taskDetail.taskName = this.crud.data[i].taskName
        // 任务照片总数
        taskDetail.numImage = this.crud.data[i].numImage
        let numBoxTotal = 0
        let in_completeTaskNum = 0
        let auditnum = 0
        // 计算框总数
        for (let j = 0; j < this.crud.data[i].images.length; j++) {
          console.log('进入循环')
          numBoxTotal += this.crud.data[i].images[j].numBox
          if (this.crud.data[i].images[j].status === 0) {
            in_completeTaskNum++
          }
          if (this.crud.data[i].images[j].status === 4 || this.crud.data[i].images[j].status === 0 ) {
            auditnum++
          }
        }
        taskDetail.markTaskId = this.crud.data[i].markTaskId
        taskDetail.numBoxtotal = numBoxTotal
        taskDetail.do = this.crud.data[i].numFinish
        taskDetail.in_complete = in_completeTaskNum
        taskDetail.createTime = this.crud.data[i].createTime
        taskDetail.finishTime = this.crud.data[i].finishTime
        taskDetail.auditNum = auditnum
        taskDetail.status = this.crud.data[i].status
        taskDetail.users = this.crud.data[i].users
        this.taskDetail.push(taskDetail)
      }
      console.log('下面是处理后的数据')
      console.log(this.taskDetail)
      return true
    }
  }
}
</script>

<style scoped>

  .el-table{
    overflow: auto;
  }
  .el-table .el-table__body-wrapper,
  .el-table .el-table__header-wrapper,
  .el-table .el-table__footer-wrapper{
    overflow: visible;
  }
  .el-table::after{
    position:relative !important;
  }
</style>
